<?php
	/**
	* Sermons view page action
	* 
	* Change logs
	* Sep 9, 2011 - trungdt - Add aggregate property
	* Sep 20, 2011 - trungdt - change the way we download file
	*/
	global $db;

	// 8/24/2009: Immanuel192; get module config
	$config=$db->selectObject("documentsmodule_config","location_data = '".serialize($loc)."'");
	if (!$config) {
		$config->item_per_page=50;
		$config->doc_col=0; $config->pdf_col=0;
		$config->mp3_col=0; $config->ppt_col=0;
        $config->flv_col=0; $config->xls_col=0;
		$config->aggregate=array();
		$config->not_allow_post=0;
	}

	$max_item=$config->item_per_page;
	// get module title
	// because if the client switch page, so, that is the action and exponent does not transfer $title and $views
	$temp=$db->selectObject("container","internal = '" . serialize($loc) . "'");
	if ($temp)
	{
		$title=$temp->title; 
		$view=$temp->view; 
	}
	else
	{ $title=""; $view="Default"; }
	
	$locsql = "(location_data='".serialize($loc)."'";
	if (!empty($config->aggregate)) {
		$locations = unserialize($config->aggregate);
		foreach ($locations as $source) {
			$tmploc = null;
			$tmploc->mod = $loc->mod;
			$tmploc->src = $source;
			$tmploc->int = '';
			$locsql .= " OR location_data='".serialize($tmploc)."'";
		}
	}
	$locsql .= ')';
	
	$count_post = $db->countObjects('documentsmodule_document',$locsql);

	// page split
	// cal how many posts
	$page_count=floor($count_post / $max_item);
	if ($count_post % $max_item != 0)
		$page_count++;

	// get page
	$page=1;
	if (isset($_REQUEST['page']))  $page=intval($_REQUEST['page']);
	if (($page<1) || ($page>$page_count)) $page=1;
	$start_pos=($page-1)*$max_item;

	// get sort state
	$sort=4; // =1: Show new sermon first ; =2: Show old sermon first; =3: show sermon which have largest download value ; 4: show first to last ; = 5: show last to first
	if (isset($_REQUEST['sort']))  $sort=intval($_REQUEST['sort']);
	if (($sort<1) || ($sort>5)) $sort=1;

	// okies, 
	$template = new template('documentsmodule',$view,$loc);

	// order by
	$order_by="ORDER BY date DESC";
	if ($sort==2) // old first
		$order_by="ORDER BY date";	
	else if ($sort==3) // most download first
			$order_by="ORDER BY download DESC";	
    else if ($sort == 4)
        $order_by="ORDER BY id";    

	$sermons= $db->selectObjects('documentsmodule_document',"{$locsql} {$order_by} LIMIT {$start_pos},{$max_item}");
	
	$cache_file=array();
	
	foreach ($sermons as $key=>$value){
		// for date
		$value->create_date=date('d/m/Y', $value->date); 
		/*$date_time=0;
		$date_time=$value->date;
		$datetime = date('l, F d, o g:i a', $date_time);
		// If the language is english, use the default language of yui calendar
		if (USE_LANG != 'eng_US') {
			// Translate days of the week
			$datetime=date("d/m/o",$value->date);
		}
		$value->create_date=$datetime;
		
		// update
		$sermons[$key]=$value;*/
		// for location, multi sermon module
		$tmp_loc=unserialize($value->location_data);
		$value->src=$tmp_loc->src;
		
		// for each file type, we get the file object
		$files = $db->selectObjects('file', "id = {$value->pdf_file} OR id = {$value->mp3_file} OR id = {$value->doc_file} OR id = {$value->ppt_file}" );
		foreach ($files as $k=>$v){
            if ($v->id == $value->xls_file){
                $value->pdf_filename=$v->filename;
            }
            if ($v->id == $value->flv_file){
                $value->pdf_filename=$v->filename;
            }
			if ($v->id == $value->pdf_file){
				$value->pdf_filename=$v->filename;
			}
			if ($v->id == $value->mp3_file){
				$value->mp3_filename=$v->filename;
			}
			if ($v->id == $value->doc_file){
				$value->doc_filename=$v->filename;
			}
			if ($v->id == $value->ppt_file){
				$value->ppt_filename=$v->filename;
			}
		}
	}

	$template->register_permissions(documentsmodule::permissions("",1),$loc);
	$template->assign('config', $config);
    $template->assign('sermons', $sermons);
	$template->assign('start_pos', $start_pos);
	$template->assign('moduletitle', $title);
	$template->assign('pages',$page_count);
	$template->assign('page',$page);
	$template->assign('sort',$sort);
	if (!(isset($_REQUEST['flow_has_set']) && ($_REQUEST['flow_has_set']==1)))
		exponent_flow_set(SYS_FLOW_PUBLIC,SYS_FLOW_ACTION);
	$template->output();
?>
